package com.chenyue.cm.utils;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

import java.io.IOException;
import java.util.Map;

/**
 * @author libingxing
 */
public class OkHttpUtils {

    public static String doGet(String url) throws IOException {
        OkHttpClient build = new OkHttpClient.Builder().build();
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        return build.newCall(request).execute().body().string();
    }

    public static String doPost(String url, RequestBody param) throws IOException {
        OkHttpClient build = new OkHttpClient.Builder().build();
        Request request = new Request.Builder()
                .url(url)
                .post(param)
                .build();
        return build.newCall(request).execute().body().string();
    }

    public static String doPost(String url, RequestBody param, Map<String, String> headers) throws IOException {
        OkHttpClient build = new OkHttpClient.Builder().build();
        final Request.Builder builder = new Request.Builder();
        if (headers.size() > 0) {
            for (Map.Entry<String, String> keys : headers.entrySet()) {
                builder.addHeader(keys.getKey(), keys.getValue());
            }
        }
        Request request = builder
                .url(url)
                .post(param)
                .build();
        return build.newCall(request).execute().body().string();
    }

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public static String doPostJson(String url, String json) throws IOException {
        OkHttpClient build = new OkHttpClient.Builder().build();
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        return build.newCall(request).execute().body().string();
    }

    public static String doPostJson(String url, String json, Map<String, String> headers) throws IOException {
        OkHttpClient build = new OkHttpClient.Builder().build();
        RequestBody body = RequestBody.create(JSON, json);
        final Request.Builder builder = new Request.Builder();
        if (headers.size() > 0) {
            for (Map.Entry<String, String> keys : headers.entrySet()) {
                builder.addHeader(keys.getKey(), keys.getValue());
            }
        }
        Request request = builder
                .url(url)
                .post(body)
                .build();
        return build.newCall(request).execute().body().string();
    }

    //File转RequestBody
//    MediaType mediaType=MediaType.Companion.parse("text/x-markdown; charset=utf-8");
//    File file=new File("");
//    RequestBody fileBody=RequestBody.Companion.create(file,mediaType);
//    Request request=new MultipartBody.Builder()
//            .addFormDataPart("file", file.getName(),fileBody)
//            .build();


}
